package com.smart.manager.weixin.mp.handler;


import com.smart.manager.weixin.mp.repository.WeixinMpReplyRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import me.chanjar.weixin.common.session.WxSessionManager;
import me.chanjar.weixin.mp.api.WxMpMessageHandler;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * 文本消息数据处理器
 *
 * @author Administrator
 */
@Log4j2
@Service
@RequiredArgsConstructor
public class TextMessageHandler implements WxMpMessageHandler {
    private final WeixinMpReplyRepository replyRepository;


    @Override
    public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage,
                                    Map<String, Object> context,
                                    WxMpService wxMpService,
                                    WxSessionManager sessionManager) {
        //根据关键字回复文消息
        String keyword = wxMessage.getContent();
        String toUser = wxMessage.getToUser();
        String fromUser = wxMessage.getFromUser();
        String appId = wxMpService.getWxMpConfigStorage().getAppId();
        return replyRepository.getReplyContentByKeyWord(appId, toUser, fromUser, keyword);
    }

}